import { request } from '@umijs/max';
// 查询部门列表

export async function deptpage(params) {
  return request('/api/sys/dept/page', {
    method: 'GET',
    params,
  });
}
// 删除部门
export async function deptDelete(params) {
  console.log('params:', params);
  try {
    const response = await request('/api/sys/dept/delete', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json' // 确保发送JSON格式的数据
      },
      data: params // 直接发送params对象，不需要手动字符串化
    });
    return response;
  } catch (error) {
    console.error('请求失败:', error);
    throw error; // 将错误抛出，以便在调用函数的地方处理
  }
}
// 获取部门树形列表
export async function deptListTree() {
  return request('/api/sys/dept/listTree', {
    method: 'GET',
  });
}
export async function deptAdd(params) {
  console.log('params:', params);
  try {
    const response = await request('/api/sys/dept/add', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json' // 确保发送JSON格式的数据
      },
      data: params // 直接发送params对象，不需要手动字符串化
    });
    return response;
  } catch (error) {
    console.error('请求失败:', error);
    throw error; // 将错误抛出，以便在调用函数的地方处理
  }
}
// 修改部门状态
export async function deptUpdateStatus(params: { id: string; status: number }) {
  try {
    const response = await request('/api/sys/dept/updateStatus', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      data: params
    });
    return response;
  } catch (error) {
    console.error('修改状态失败:', error);
    throw error;
  }
}
// 更新部门
export async function deptUpdate(params: {
  id: string;
  departmentName: string;
  departmentType: number;
  fatherDepartmentId: string;
  description?: string;
  sort: number;
}) {
  return request('/api/sys/dept/update', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    data: params
  });
}
// 获取部门详情
export async function deptDetail(params) {
  return request(`/api/sys/dept/detail`, {
    method: 'GET',
    params,
  });
}
// 获取部门岗位列表
export async function getDeptPositionList(params) {
  console.log('params:', params);
  return request('/api/sys/dept/positionList', {
    method: 'GET',
    params,
  });
}
// 删除岗位
export async function deletePosition(params: { departmentId: string ,positionId:string}) {
  return request('/api/sys/dept/positionDelete', {
    method: 'POST',
    data: params,
  });
}
// 岗位资源分配
export async function positionResourceAllocation(params: {
}) {
  return request('/api/sys/dept/positionResourceAllocation', {
    method: 'POST',
    data: params,
  });
}
// 获取岗位人员列表
export async function getDeptPositionListUser(params) {
  console.log('params 获取岗位人员列表 :', params);
  return request('/api/sys/dept/positionListUser', {
    method: 'GET',
    params,
  });
}
// 获取岗位列表
export async function getPostList(params) {
  return request<API.PostListResult>('/api/sys/position/list', {
    method: 'GET',
    params,
  });
}
// 添加岗位
export async function positionAdd(params) {
  return request('/api/sys/dept/positionAdd', {
    method: 'POST',
    data: params,
  });
}
// 获取资源树形列表
export async function getPositionResources(params) {
  return request('/api/sys/dept/getResourceTreeWithDeptPosition', {
    method: 'GET',
    params,
  });
}
// 获取资源分配
export async function getPositionResourceAllocation(params: { deptId: string }) {
  return request('/api/system/position/resource/get', {
    method: 'GET',
    params,
  });
}
